package cn.baage.campus.controller;

import cn.baage.campus.model.po.Category;
import cn.baage.campus.service.CategoryService;
import cn.baage.campus.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 分类API
 * @Author: 八阿哥
 * @url: baage.cn
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/category")
@Api(tags = "分类接口")
@AllArgsConstructor(onConstructor_ = @Autowired)
public class CategoryController {

    private final CategoryService categoryService;


    @GetMapping("/listAll")
    @ApiOperation(value = "获取全部分类")
    public Result<Object> listAllCategory() {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getOrderNum);
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        Map<String, Object> res = new HashMap<String,Object>(){{
           put("categoryList", list);
        }};
        return Result.success(res);
    }


    @PostMapping("/add")
    @ApiOperation(value = "新增分类")
    public Result<Category> addCategory(@RequestBody @Validated Category category) {
        categoryService.addCategory(category);
        return Result.success();
    }


    @PostMapping("update")
    @ApiOperation(value = "更新分类")
    public Result<Object> updateCategory(@RequestBody @Validated Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }


}
